<?

class Miniplanet
{
    public function __construct()
    {
        require 'config.php';

        require realpath(dirname($_SERVER['SCRIPT_FILENAME']) . '/../') . '/application/config/' . 'global.php';
        
        require 'debug.php';
        
        require 'controller.php';

        require 'model.php';

		require 'view.php';

        /*
         * 配置时区
         */
        @date_default_timezone_set("Asia/shanghai");

        /*
         * 配置包头信息
         */
        header("Content-type:text/html; charset=utf-8;");
        
        define('domain', $_SERVER['HTTP_HOST']);
	
        define('is_ajax', !empty($_REQUEST['is_ajax']) ? $_REQUEST['is_ajax'] : false);
	
        define('is_jsonp', !empty($_REQUEST['is_jsonp']) ? $_REQUEST['is_jsonp'] : false);
		
        define('referer', !empty($_REQUEST['referer']) ? $_REQUEST['referer'] : (!empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : './'));
        
        /*
         * 定义动态加载方法
         */
        spl_autoload_register(array($this, 'autoLoad'));
        
        set_exception_handler(function($_exception){
            Debug::error($_exception);
        });
    }
    
    private function autoLoad($_class)
    {
        if(class_exists($_class)) return ;
        
        $class_name_A = explode('_', strtolower($_class));
        
        switch (array_shift($class_name_A))
        {
            case 'controller':
                $class_path = path_prefix_application . 'controller/' . implode('/', $class_name_A) . '.php';
            break;
            
            case 'library':
                $class_path = path_prefix_application . 'library/' . implode('/', $class_name_A) . '.php';
            break;
        
            case 'model':
                $class_path = path_prefix_application . 'model/' . implode('/', $class_name_A) . '.php';
            break;
        }
        
        if(!empty($class_path))
        {
            if(file_exists($class_path)||develop)
            {
                require $class_path;
            }
            else
            {
                header("Location:" . error_page_url);
            }
        }
    }

    public function start()
    {
        // 开始时间
		Debug::time('frame_start');
        
		require 'route.php';
        
        new Route();
    }

    public function finish()
    {
        // 结束时间
		Debug::timeEnd('frame_start');
        
        Debug::memoryUsage();
    }
}

?>